package com.bkhech.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import com.bkhech.RequestConstant;
import com.bkhech.grace.result.GraceJSONResult;
import com.bkhech.pojo.FriendCircleLiked;
import com.bkhech.pojo.bo.FriendCircleBO;
import com.bkhech.pojo.vo.FriendCircleVO;
import com.bkhech.service.FriendCircleService;
import com.bkhech.utils.PagedGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/friendCircle")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FriendCircleController {
    private final FriendCircleService friendCircleService;

    @PostMapping("/publish")
    public GraceJSONResult<Void> publish(@RequestBody FriendCircleBO friendCircleBO, HttpServletRequest request) {
        String userId = request.getHeader(RequestConstant.HEADER_USER_ID);
        if (StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        friendCircleBO.setUserId(userId);
        friendCircleBO.setPublishTime(LocalDateTime.now());

        friendCircleService.publish(friendCircleBO);
        return GraceJSONResult.ok();
    }

    @PostMapping("/queryList")
    public GraceJSONResult<PagedGridResult<FriendCircleVO>> queryList(String userId, @RequestParam(defaultValue = "1", name = "page") Integer page,
                                     @RequestParam(defaultValue = "10", name = "pageSize") Integer pageSize) {
        if (StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        Page<FriendCircleVO> pageInfo = new Page<>(page, pageSize);
        PagedGridResult<FriendCircleVO> pageResult = friendCircleService.findByPage(userId, pageInfo);
        return GraceJSONResult.ok(pageResult);
    }

    @PostMapping("/like")
    public GraceJSONResult<Void> like(String friendCircleId, HttpServletRequest request) {
        String userId = request.getHeader(RequestConstant.HEADER_USER_ID);
        if (StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        friendCircleService.like(userId, friendCircleId);
        return GraceJSONResult.ok();
    }

    @PostMapping("/unlike")
    public GraceJSONResult<Void> unlike(String friendCircleId, HttpServletRequest request) {
        String userId = request.getHeader(RequestConstant.HEADER_USER_ID);
        if (StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        friendCircleService.unlike(userId, friendCircleId);
        return GraceJSONResult.ok();
    }

    @PostMapping("/likedFriends")
    public GraceJSONResult<List<FriendCircleLiked>> likedFriends(String friendCircleId) {
        if (StringUtils.isBlank(friendCircleId)) {
            return GraceJSONResult.error();
        }
        List<FriendCircleLiked> friendCircleLikeds = friendCircleService.queryLikedFriend(List.of(friendCircleId));
        return GraceJSONResult.ok(friendCircleLikeds);
    }

    @PostMapping("/delete")
    public GraceJSONResult<Void> delete(String friendCircleId, HttpServletRequest request) {
        String userId = request.getHeader(RequestConstant.HEADER_USER_ID);
        if (StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        friendCircleService.delete(friendCircleId, userId);
        return GraceJSONResult.ok();
    }


}
